#include"queue.h"
void queueInit(queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}

void queueDestroy(queue* pq)
{
	assert(pq);
	QlistNode* cur = pq->head;
	while (cur)
	{
		QlistNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = NULL;
	pq->tail = NULL;
}
void queuepush(queue* pq, Qdatatype x)
{
	assert(pq);
	QlistNode* newqueue = (QlistNode*)malloc(sizeof(QlistNode));
	if (newqueue == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newqueue->data = x;
	newqueue->next = NULL;
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newqueue;
	}
	else
	{
		pq->tail->next = newqueue;
		pq->tail = newqueue;
	}
}

void queuepop(queue* pq)
{
	assert(pq);
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = NULL;
		pq->tail = NULL;
	}
	else
	{
		QlistNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

bool queueempty(queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

Qdatatype queuefront(queue* pq)
{
	assert(pq);
	assert(!queueempty(pq));
	return pq->head->data;
}

Qdatatype queueback(queue* pq)
{
	assert(pq);
	assert(!queueempty(pq));
	return pq->tail->data;
}

int queuesize(queue* pq)
{
	assert(pq);
	QlistNode* cur = pq->head;
	int count = 0;
	while (cur)
	{
		++count;
		cur = cur->next;
	}

	return count;
}

void print(queue* pq)
{
	assert(pq);
	QlistNode* cur = pq->head;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
}